package cn.sccl.dirms.security.service.impl;

import cn.sccl.dirms.security.model.Role;
import cn.sccl.common.dao.BaseDAO;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.dirms.security.dao.RoleDAO;
import cn.sccl.dirms.security.service.RoleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@Service
public class RoleManagerImpl extends BaseManagerImpl<Role> implements RoleManager {

    @Autowired
    private RoleDAO roleDAO;

    public RoleDAO getRoleDAO() {
        return roleDAO;
    }

    @Override
    public BaseDAO<Role> getBaseDAO() {
        return getRoleDAO();
    }

    @Override
    public Role findByName(String name) {
        Map queryMap = new HashMap();
        queryMap.put("name", name);
        return this.findUniqueBy(queryMap);
    }

    @Override
    public List<Role> findRoleData(String id) {
        String sql = "select r.name,r.id\n" +
                "  from t_sec_role       r,\n" +
                "       t_sec_users_roles      a\n" +
                " where  a.role_id = r.id\n" +
                "   and  a.user_id = ?\n" +
                "   and  r.delete_flag = 0\n" +
                "   and  a.delete_flag = 0";
        List<Role> roleList = getDataBase().query4Model(sql, Role.class, new Object[]{id});
        HashSet<Role> hs = new HashSet<Role>(roleList);//通过hashset来去除list中重复的元素
        roleList.clear();
        roleList.addAll(hs);
        return roleList;
    }
}